< previous page page_679 next page >

Page 679
9f31265bb40b592b1929e18a47ab28e5.gif
      strName = Forms(intLoop).Name
   Next
Next
In my tests, the code using the For...Each construct runs at least twice as fast:
9f31265bb40b592b1929e18a47ab28e5.gif
For lngLoop = 0 To 10000
   For Each oForm In Forms
      strName = oForm.Name
   Next
Next
This runs faster because the enumeration function keeps track of which object to retrieve when For...Each is used. However, when you access the collection using an index or a key value, a search must be performed through that collection every time you access it. This can take a while because searching through collections seems to be done sequentially.
With. . .End With
Using the With...End With construct is slightly faster than not using it. In Visual Basic 5, this construct seemed to be much faster than it does now in Visual Basic 6. Microsoft probably did some optimization underneath the hood. Heres a sample of the code where an object is declared of the type Employee, called oEmp. In the first routine, properties are set on the Employee object and then are retrieved back. In each line, the Employee object is referenced:
9f31265bb40b592b1929e18a47ab28e5.gif
For lngLoop = 0 To conRepeats
   oEmp.EmpID = 1
   oEmp.FirstName = "Bill"
   oEmp.LastName = "Gates"
   oEmp.Salary = 100000
   strValue = oEmp.EmpID
   strValue = oEmp.FirstName
   strValue = oEmp.LastName
   strValue = oEmp.Salary
Next
In the next example, the With...End With construct is applied to the oEmp variable prior to going into the loop:
9f31265bb40b592b1929e18a47ab28e5.gif
With oEmp
   For lngLoop = 0 To conRepeats

 
< previous page page_679 next page >

If you like this book, buy it!